package com.mdc.mansion.api.web.modules.restaurant;

import com.alibaba.fastjson.JSONObject;
import com.esotericsoftware.minlog.Log;
import com.mdc.mansion.api.web.common.interceptor.NoLoginCheck;
import com.mdc.mansion.api.web.common.utils.login.LoginTicket;
import com.mdc.mansion.api.web.common.utils.login.LoginUtil;
import com.mdc.mansion.api.web.common.web.BaseController;
import com.mdc.mansion.common.exception.BusinessException;
import com.mdc.mansion.common.response.Result;
import com.mdc.mansion.common.response.ResultCode;
import com.mdc.mansion.common.util.I18nUtil;
import com.mdc.mansion.core.modules.restaurant.RestaurantService;
import com.mdc.mansion.core.modules.restaurant.RestaurantStaffService;
import com.mdc.mansion.core.modules.restaurant.dto.RestaurantDTO;
import com.mdc.mansion.dataaccess.common.context.ContextHolder;
import com.mdc.mansion.dataaccess.modules.restaurant.entity.Restaurant;
import com.mdc.mansion.dataaccess.modules.restaurant.entity.RestaurantStaff;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping("${adminPath}/restaurant")
public class RestaurantController extends BaseController {

    @Autowired
    private RestaurantService restaurantService;

    @Autowired
    private RestaurantStaffService restaurantStaffService;

    @ModelAttribute("restaurantDTO")
    public RestaurantDTO getRestaurantDTO(@RequestParam(required = false) String data) {
        if(StringUtils.isBlank(data)){return null;}
        RestaurantDTO restaurantDTO= JSONObject.parseObject(data,RestaurantDTO.class);
        return restaurantDTO;
    }

    @RequestMapping("get")
    public Result getInfo(){
        Result result=new Result(ResultCode.COMMON_SUCCESS);
        RestaurantDTO restaurantDTO=restaurantService.currentUserRestaurant();
        result.setProperty("restaurant",restaurantDTO);
        return result;
    }

    @RequestMapping("save")
    public Result save(@ModelAttribute("restaurantDTO")RestaurantDTO restaurantDTO){
        Result result=new Result(ResultCode.COMMON_SUCCESS);
        restaurantService.saveCurrentUserRestaurant(restaurantDTO);
        return result;
    }

    @RequestMapping("h5/takeToken")
    @NoLoginCheck
    public Result takeToken(String data){
        Result restultResult=new Result(ResultCode.COMMON_SUCCESS);
        if(StringUtils.isBlank(data)){throw new BusinessException(I18nUtil.getMessage("param.error"));}
        JSONObject jsonObject=JSONObject.parseObject(data);
        String restaurantId = jsonObject.getString("restaurantId");
        if(StringUtils.isBlank(restaurantId)){throw new BusinessException(I18nUtil.getMessage("param.error"));}
        RestaurantStaff restaurantStaff=new RestaurantStaff();
        restaurantStaff.setRestaurantId(restaurantId);
        restaurantStaff.setRoleType(RestaurantStaff.ROLE_TYPE.FWY.getVal());
        List<RestaurantStaff> restaurantStaffs = restaurantStaffService.findList(restaurantStaff);
        if(CollectionUtils.isEmpty(restaurantStaffs)){
            restaurantStaff.setRoleType(RestaurantStaff.ROLE_TYPE.SYY.getVal());
            restaurantStaffs = restaurantStaffService.findList(restaurantStaff);
        }
        if(CollectionUtils.isEmpty(restaurantStaffs)){throw new BusinessException(I18nUtil.getMessage("param.error"));}
            restultResult.setProperty("token", restaurantStaffs.get(0).getLoginAccount());
            return restultResult;
        }
}
